/**
 * Created by DevilsEyes on 2016/2/14.
 */
'use strict';
var express = require('express');
var router = express.Router();

var $doc = require('../model/doc.js');
var $stats = require('../model/stats.js');

var manager = require('../task/manager.js');
var antiBaidu = require('../task/anti-baidu.js');

/* GET home page. */
router.get('/clear', function (req, res, next) {
    $stats.clear();
    $doc.clear(()=> {
    });

    res.render('index', {message: '清除完毕'});
});


router.get('/update', function (req, res, next) {

    $doc.$Model
        .find({id: '1455947089717_2'})
        .update({title: '随便更新下', status: 0})
        .exec(function (err) {
            res.render('index', {message: '更新完毕'});
        });

});

router.get('/all', function (req, res, next) {
    $doc.getAll()
        .then((docs)=> {
            $stats.get(function (err, stats) {
                res.render('table', {
                    list: docs,
                    stats: stats
                });
            });
        })

});

router.get('/distinct', function (req, res, next) {
    $doc.$Model.find().distinct('link', function (err, docs) {
        console.log(docs);
    })

});

router.get('/watch', function (req, res, next) {
    let filter = (obj)=> {
        let nobj = Object.create(obj);
        for (let o of nobj.Manager.list) {
            o.timer = null;
        }
        return nobj;
    };

    res.json(filter(manager.watch()));
});

router.get('/antiBaidu', function (req, res, next) {
    antiBaidu.start();
    res.send('OK');
});

module.exports = router;